<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>判断变量类型</title>
  </head>
  <body>
    <script>
      var str = "123"; //String
      var num = 123; //number
      var arry = [1, 2, 3]; //Arry
      var bool = true; //Boolean;
      var obj = {
        num: 123,
      }; //Object
      var und = undefined; //undefined
      var nu = null; //null

      //1. typeof
      console.log("-----------");
      console.log("通过typeof来判断类型");
      console.log("变量str的数据类型：" + typeof str);
      console.log("变量num的数据类型：" + typeof num);
      console.log("变量arry的数据类型：" + typeof arry);
      console.log("变量bool的数据类型：" + typeof bool);
      console.log("变量obj的数据类型：" + typeof obj);
      console.log("变量und的数据类型：" + typeof und);
      console.log("变量nu的数据类型：" + typeof nu);
      //2.instanceof和constructor
      console.log("-----------");
      console.log("通过instanceof和constructor来判断类型");
      console.log("变量arry的数据类型是Arry吗：" + (arry instanceof Array));
      console.log("变量arry的数据类型是Arry吗：" + (arry.constructor == Array));
      //3.通过Object.prototype.toString.call
      console.log("-----------");
      console.log("通过通过Object.prototype.toString.call来判断类型");
      console.log("变量str的数据类型：" + Object.prototype.toString.call(str));
      console.log("变量num的数据类型：" + Object.prototype.toString.call(num));
      console.log("变量arry的数据类型：" + Object.prototype.toString.call(arry));
      console.log("变量bool的数据类型：" + Object.prototype.toString.call(bool));
      console.log("变量obj的数据类型：" + Object.prototype.toString.call(obj));
      console.log("变量und的数据类型：" + Object.prototype.toString.call(und));
      console.log("变量nu的数据类型：" + Object.prototype.toString.call(nu));
    </script>
  </body>
</html>
